import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Users from '../components/user/users.vue'
import Roles from '../components/roles.vue'
import TaskList from '../components/tasklist.vue'
import Editor  from '../components/element/editor.vue'
import Cours from '../components/element/cours.vue'

Vue.use(VueRouter)

const routes = [
  { path:'/',redirect:'/login'},
 { path:'/login',component:Login},
 {path:'/home',
 component:Home,redirect:'/welcome',
 children:[
{path:'/welcome',component:Welcome},
{path:'/users',component:Users},
{path:'/roles',component:Roles},
{path:'/tasklist',component:TaskList},
 ]},
 { path:'/editor',component:Editor},
 { path:'/cours',component:Cours},
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  // to去哪里，from从哪个路径跳转来，next是一个函数，表示放行，next()放行 next('/login')强制跳转
  if(to.path=='/login') return next();
  //获取token
  const tokenStr=window.localStorage.getItem('token')
  if(!tokenStr) return next('/login');
  next()
})

export default router
